﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class group_creategroup : Wozuji.Logic.PageLogin
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Header.Title = "足迹网 创建组群";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (TNameValid())
        {
            if (TDescValid())
            {
                if (t_groupclassValid())
                {
                    if (logoValid())
                    {

                        if (TDescValid())
                        {
                            if (logoValid())
                            {
                                Wozuji.Model.Team_t tm = new Wozuji.Model.Team_t();

                                //判断是否有logo，如有先上传再做处理

                                if (T_logo.HasFile)
                                {
                                    T_logo.PostedFile.SaveAs(Server.MapPath("/img/Team/logo/") + T_logo.FileName);
                                    tm.Team_Logo = "/img/Team/logo/" + T_logo.FileName;
                                }
                                else
                                {
                                    tm.Team_Logo = "/img/Team/logo/default.png";
                                }
                                tm.Team_Admin = Convert.ToInt32(Session["userid"]);
                                tm.Team_Name = T_name.Text.Trim();
                                tm.Team_description = T_desc.Text.Trim();

                                tm.Team_BigClass = Convert.ToInt32(t_class.SelectedItem.Value);
                                tm.Team_CreateTime = DateTime.Now;

                                tm.Team_BigClass = Convert.ToInt32(t_class.SelectedItem.Value);

                                Wozuji.Logic.groups gp = new Wozuji.Logic.groups();
                                int teamid = gp.createTeam(tm);
                                if (teamid > 0)
                                {

                                    Wozuji.Common.MessageBox.ShowAndRedirect(Page, "组群添加成功！", "invisit.aspx?tid=" + teamid.ToString());
                                }
                                else
                                {
                                    Wozuji.Common.MessageBox.Show(Page, "组群添加失败，请稍后再试");
                                }

                            }
                        }

                    }
                }
            }
        }



    }


    public bool logoValid()
    {
        bool filetype = false;
        if (T_logo.HasFile)
        {
            //判断文件类型
            string fileExtension = System.IO.Path.GetExtension(T_logo.FileName).ToLower();
            //限定只能上传jpg和gif图片
            string[] allowExtension = { ".jpg", ".gif", ".png", ".bmp" };

            //对上传的文件的类型进行一个个匹对
            for (int i = 0; i < allowExtension.Length; i++)
            {
                if (fileExtension == allowExtension[i])
                {
                    filetype = true;

                }

            }
            if (!filetype)
            {
                logoValidMsg.Text = "不是图片文件";
                return false;
            }
            //判断文件大小
            else if (T_logo.FileContent.Length > 200000)
            {
                logoValidMsg.Text = "图片大小不能超过200K";
                return false;
            }
            else
            {
                logoValidMsg.Text = "";
                return true;
            }


        }
        else
        {
            return true;
        }
    }
    public bool TNameValid()
    {
        //组群名称校验
        Wozuji.Logic.groups gp = new Wozuji.Logic.groups();
        if (T_name.Text.Trim().Equals(""))
        {
            nameValid.Text = "请输入名称";
            return false;
        }
        else if (T_name.Text.Trim().Length > 50)
        {
            nameValid.Text = "不能超过50个字哦";
            return false;
        }
        else if (gp.teamExist(T_name.Text.Trim()))
        {
            nameValid.Text = "已经存在，换个其他名字哦";
            return false;
        }
        else
        {
            nameValid.Text = "";
            return true;
        }

    }

    public bool TDescValid()
    {
        //组群名称校验

        if (T_desc.Text.Trim().Equals(""))
        {
            descValid.Text = "请输入描述";
            return false;
        }
        else if (T_desc.Text.Trim().Length > 200)
        {
            descValid.Text = "不能超过50个字哦";
            return false;
        }

        else
        {
            descValid.Text = "";
            return true;
        }

    }

    public bool t_groupclassValid()
    {

        try
        {
            string selitem = t_class.SelectedItem.Value;
            classvalidmsg.Text = "";
            return true;

        }
        catch (Exception ex)
        {
            classvalidmsg.Text = "请选择组群类别";
            return false;

        }
    }

}
